import 'dart:math';

import 'package:flutter/animation.dart';
import 'package:get/get.dart';
import 'package:oli/services/Apis.dart';
import 'package:oli/services/ApiService.dart';
import 'package:oli/widget/Shape/RandomCircle.dart';
import 'package:oli/widget/Shape/RandomSquare.dart';
import 'package:oli/widget/Shape/RandomTriangle.dart';

import 'state.dart';

class LoginLogic extends GetxController with GetSingleTickerProviderStateMixin {
  final LoginState state = LoginState();
  late AnimationController controller;
  final Random _random = Random();
  @override
  void onInit() {
    super.onInit();
    controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 16),
    )..repeat();
    state.shapes.addAll(
      List.generate(20, (index) {
        Offset position = Offset(
          _random.nextDouble() * 300,
          _random.nextDouble() * 600,
        );
        double speedX = (_random.nextDouble() * 2 - 1) * 2;
        double speedY = (_random.nextDouble() * 2 - 1) * 2;
        Color color = Color.fromRGBO(
          _random.nextInt(256),
          _random.nextInt(256),
          _random.nextInt(256),
          0.5,
        );
        int shapeType = _random.nextInt(3);
        switch (shapeType) {
          case 0:
            return RandomCircle(
              position: position,
              speedX: speedX,
              speedY: speedY,
              color: color,
              radius: _random.nextDouble() * 10 + 5,
            );
          case 1:
            return RandomTriangle(
              position: position,
              speedX: speedX,
              speedY: speedY,
              color: color,
              sideLength: _random.nextDouble() * 10 + 5,
            );
          case 2:
            return RandomSquare(
              position: position,
              speedX: speedX,
              speedY: speedY,
              color: color,
              sideLength: _random.nextDouble() * 10 + 5,
            );
          default:
            return RandomCircle(
              position: position,
              speedX: speedX,
              speedY: speedY,
              color: color,
              radius: _random.nextDouble() * 10 + 5,
            );
        }
      }),
    );
    controller.addListener(() {
      final size = Get.size;
      for (var shape in state.shapes) {
        shape.move(size);
        shape.updateVisibility(size);
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    controller.dispose();
  }

  void toggleLoginRegister() {
    state.isLogin.value = !state.isLogin.value;
    update(); // 触发界面更新
  }

  void reg() {
    ApiService.postRequest(Apis.reg, {
      "email": state.emailRegisterController.value.text,
      "passwd": state.passwordRegisterController.value.text,
      "code": "",
      "codeKey": "",
    });
  }

  void login() {
    ApiService.postRequest(Apis.login, {
      "email": state.emailLoginController.value.text,
      "passwd": state.passwordLoginController.value.text,
      "code": "",
      "codeKey": "",
    });
  }

  void getCode() async {
    // 调用获取验证码的接口
    final res = await ApiService.postRequest(Apis.getCode, {});
    if (res.code == 2000) {
      List<String> parts = res.data.split('--|--');
      if (parts.length == 2) {
        state.codeKey.value = parts[0];
        state.captchaImageUrl.value = parts[1];
      }
    }
  }
}
